﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model.Entities;
using NHibernate;
using Totoro.ORMapping;

namespace Bussiness.ModelQuery
{
    public class ProductCommentManager
    {
        public ProductCommentManager()
        {
 
        }
        private static ProductCommentManager instance;

        public static ProductCommentManager Instance
        {
            get 
            {
                if (instance == null)
                {
                    instance = new ProductCommentManager();
                    instance.LoadData();
                }
                return instance;
            }
        }
        Dictionary<string, ProductComments> proCometCaches = new Dictionary<string, ProductComments>();
        public void LoadData()
        {
            proCometCaches.Clear();
            ISession session = NHHelper.GetCurrentSession();
            ICriteria crt = session.CreateCriteria<ProductComments>();
            IList<ProductComments> proCometList = null;
            proCometList = crt.List<ProductComments>();
            if (proCometList != null)
            {
                foreach (ProductComments productComment in proCometList)
                {
                    proCometCaches.Add(productComment.ID, productComment);
                }
            }
        }
        /// <summary>
        /// 根据ID获取订单的评论
        /// </summary>
        /// <param name="sId">评论Id</param>
        /// <returns></returns>
        public ProductComments GetProductCommentById(string sId)
        {
            if (proCometCaches.ContainsKey(sId))
            {
                return proCometCaches[sId];
            }
            return null;
        }
        /// <summary>
        /// 获取所有的订单评论
        /// </summary>
        /// <returns></returns>
        public ProductComments[] GetProductComments()
        {
            return proCometCaches.Values.ToArray();
        }
        /// <summary>
        /// 根据明细订单Id获取订单的评论信息
        /// </summary>
        /// <param name="sOrderDetailId">明细订单Id</param>
        /// <returns></returns>
        public ProductComments GetProductCommentByOrderDetailId(string sOrderDetailId)
        {
            foreach (ProductComments productComment in proCometCaches.Values)
            {
                if (productComment.OrderDetailID == sOrderDetailId)
                {
                    return productComment;
                }
            }
            return null;
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sId"></param>
        public void DeleteProdcutCommentByID(string sId)
        {
            ISession session = NHHelper.GetCurrentSession();
            ProductComments productComment = GetProductCommentById(sId);
            if (productComment != null)
            {
                session.Delete(productComment);
                proCometCaches.Remove(sId);
            }
        }
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="productComment"></param>
        public void InsertProdcutComment(ProductComments productComment)
        {
            NHHelper.SaveOjb(productComment);
            proCometCaches.Add(productComment.ID, productComment);
        }
    }
}
